﻿@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    string toolbarItem = ViewBag.ToolbarItem ?? "add,edit,save,cancel,delete";
}
@section header{
<style type="text/css">
    html { font-family: sans-serif; }
    .datagrid-header-inner { font-weight: bold; }
</style>
}
@section footer{
    <script src="/Scripts/plugins/datagrid-filter.js" type="text/javascript"></script>
    <script src="/Scripts/plugins/datagrid-detailview.js" type="text/javascript"></script>
    <script type="text/javascript">
        var rownumbers = true, singleSelect = false, ctrlSelect = true, multiSort = false, pageSize = 25;
        var grid, frozenColumns = [[]], columns = [[]], filterData = [], enableFilterData = false, editIndex = undefined, columnMenu = undefined;

        var startfunction = function () {
        };
        var endfunction = function () {
        };
        var addObject = function () {
            return {};
        };
        var replaceSearchField = function (field) {
            return field;
        };
    </script>
    @RenderSection("paramInit", false)
    <script type="text/javascript">
        function formatBoolean(value) {
            var icon = value ? 'checkmark' : 'checknomark';
            return '<span class="tree-file icon-' + icon + '"></span>';
        }

        var addNewRow = function () {
            if (!endEditing()) {
                $.osharp.easyui.msg.tip("请先提交或取消正在编辑的行。");
                return;
            }
            grid.datagrid("appendRow", addObject() || {});
            editIndex = grid.datagrid("getRows").length - 1;
            grid.datagrid("selectRow", editIndex)
                .datagrid("beginEdit", editIndex);
        };

        var beginEdit = function () {
            var row = grid.datagrid("getSelected");
            if (!row) {
                $.osharp.easyui.msg.tip("请选择要编辑的行。");
                return;
            }
            var index = grid.datagrid("getRowIndex", row);
            beginEditRow(index);
        };

        var beginEditRow = function (index) {
            @if (toolbarItem == null || !toolbarItem.Contains(",save"))
            {
                @Html.Raw("return;")
            }

            if (endEditing()) {
                grid.datagrid("selectRow", index)
                    .datagrid("beginEdit", index);
                editIndex = index;
            } else {
                grid.datagrid("unselectRow", index)
                    .datagrid("selectRow", editIndex);
            }
        };

        var cancelEdit = function () {
            grid.datagrid("rejectChanges");
            editIndex = undefined;
        };

        var saveChanges = function () {
            if (!endEditing()) {
                return;
            }
            var adds = grid.datagrid("getChanges", "inserted");
            if (adds && adds.length > 0) {
                submitAdds(adds);
            }
            var edits = grid.datagrid("getChanges", "updated");
            if (edits && edits.length > 0) {
                submitEdits(edits);
            }
        };

        function endEditing() {
            if (editIndex == undefined) {
                return true;
            }
            if (grid.datagrid("validateRow", editIndex)) {
                grid.datagrid("endEdit", editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }

        function submitAdds(objs) {
            $.post("@ViewBag.AddUrl", { dtos: JSON.stringify(objs) }, ajaxResultHandler);
        }

        function submitEdits(objs) {
            $.post("@ViewBag.EditUrl", { dtos: JSON.stringify(objs) }, ajaxResultHandler);
        }

        var deleteRows = function () {
            var selectRows = grid.datagrid("getSelections");
            if (selectRows.length == 0) {
                $.osharp.easyui.msg.tip("请先选中要删除的行。");
                return;
            }
            var ids = $.Enumerable.From(selectRows).Select(function (m) { return m.Id; }).ToArray();
            $.osharp.easyui.msg.confirm("是否要删除所有选中的行？此操作是不可恢复的。", null, function () {
                $.post("@ViewBag.DeleteUrl", { ids: JSON.stringify(ids) }, ajaxResultHandler);
            });
        };

        function ajaxResultHandler(data) {
            if (data.Type == "Success") {
                grid.datagrid("reload");
            }
            if (data.Type == "Error") {
                $.osharp.easyui.msg.error(data.Content);
            } else {
                $.osharp.easyui.msg.tip(data.Content);
            }
        }

        var toolbarData = [
            @if (toolbarItem.Contains("add"))
            {
                @:{ text: "增加", iconCls: "icon-add", handler: addNewRow },
            }
            @if (toolbarItem.Contains("edit"))
            {
            <text>
            { text: "编辑", iconCls: "icon-edit", handler: beginEdit },
            "-",
            </text>
            }
            @if (toolbarItem.Contains("save"))
            {
                @:{ text: "保存", iconCls: "icon-save", handler: saveChanges },
            }
            @if (toolbarItem.Contains("cancel"))
            {
                <text>
            { text: "取消", iconCls: "icon-undo", handler: cancelEdit },
            "-",
            </text>
            }
            @if (toolbarItem.Contains("delete"))
            {
                @:{ text: "删除", iconCls: "icon-remove", handler: deleteRows },
            }
        ];
    </script>
    @RenderSection("customScript", true)
    <script type="text/javascript">
        $(function () {
            startfunction();

            grid = $("#grid-@ViewBag.GridId").datagrid({
                title: "@ViewBag.Title",
                fit: true,
                frozenColumns: frozenColumns,
                columns: columns,
                fitColumns: false,
                url: "@ViewBag.GridDataUrl",
                loadMsg: "正在加载数据，请稍候",
                toolbar: toolbarData,
                rownumbers: rownumbers,
                singleSelect: singleSelect,
                ctrlSelect: ctrlSelect,
                multiSort: multiSort,
                pagination: true,
                pageSize: pageSize,
                pageList: [10, 25, 50, 100, 200],
                remoteFilter: true,
                onBeforeLoad: beforeLoad,
                loadFilter: loadFilter,
                onLoadError: loadError,
                onDblClickRow: beginEditRow,
                onHeaderContextMenu: headerContextMenu,
                showFooter: true
            });
            if (enableFilterData) {
                grid.datagrid("enableFilter", filterData);
            }
            
            endfunction();
        });

        //Header右键
        function headerContextMenu(e) {
            e.preventDefault();
            if (!columnMenu) {
                createColumnMenu();
            }
            columnMenu.menu("show", { left: e.pageX, top: e.pageY });
        }

        function createColumnMenu() {
            columnMenu = $("<div/>").appendTo("body");
            columnMenu.menu({
                onClick: function (item) {
                    if (item.iconCls == "icon-checkmark") {
                        grid.datagrid("hideColumn", item.name);
                        columnMenu.menu("setIcon", { target: item.target, iconCls: "icon-checknomark" });
                    } else {
                        grid.datagrid("showColumn", item.name);
                        columnMenu.menu("setIcon", { target: item.target, iconCls: "icon-checkmark" });
                    }
                }
            });
            var fields = grid.datagrid("getColumnFields");
            for (var i = 0; i < fields.length; i++) {
                var field = fields[i];
                var col = grid.datagrid("getColumnOption", field);
                columnMenu.menu("appendItem", { text: col.title, name: field, iconCls: col.hidden ? "icon-checknomark" : "icon-checkmark" });
            }
        }

        //param的部分属性与后台要求不符，重置属性并删除原有属性
        function beforeLoad(param) {
            if (param.page) {
                param.pageIndex = param.page;
                delete param.page;
            }
            if (param.rows) {
                param.pageSize = param.rows;
                delete param.rows;
            }
            if (param.sort) {
                var array = param.sort.split(',');
                for (var i = 0; i < array.length; i++) {
                    var field = array[i];
                    array[i] = replaceSearchField(field);
                }
                param.sort = $.osharp.tools.array.expandAndToString(array, ",");
                param.sortField = param.sort;
                delete param.sort;
            }
            if (param.order) {
                param.sortOrder = param.order;
                delete param.order;
            }
            if (param.filterRules) {
                if (param.filterRules != "[]") {
                    param.where = getFilterGroup(param.filterRules);
                }
                delete param.filterRules;
            }
        }

        function getFilterGroup(filterRules) {
            var group = new $.osharp.filter.group();
            var rules = eval(filterRules);
            for (var i = 0; i < rules.length; i++) {
                var rule = rules[i];
                rule.field = replaceSearchField(rule.field);
                rule.op = rule.op == "beginwith" ? "startswith" : rule.op == "endwith" ? "endswith" : rule.op;

                group.Rules.push(new $.osharp.filter.rule(rule.field, rule.value, rule.op));
            }
            return JSON.stringify(group);
        }

        function loadFilter(data) {
            if (data.Type != undefined && data.Type == "Error") {
                $.osharp.easyui.msg.error(data.Content);
                data.rows = [];
                data.total = 0;
                return data;
            }
            if (data.Rows != undefined && data.Total != undefined) {
                data.rows = data.Rows;
                data.total = data.Total;
                delete data.Rows;
                delete data.Total;
            }
            return data;
        }

        function loadError() {
            $.osharp.easyui.msg.error("远程数据载入失败，请重试或检查参数。");
        }

    </script>
    @RenderSection("endScript", false)
}

@RenderSection("headHtml", false)
<div id="grid-@ViewBag.GridId"></div>
@RenderSection("footHtml", false)